package ssm.service.impl;

import org.springframework.stereotype.Service;
import ssm.dao.IUserDao;
import ssm.pojo.Pager;
import ssm.pojo.User;
import ssm.service.IUserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author: silence
 * Date: 17-5-26.
 */
@Service("userService")
public class UserServiceImpl implements IUserService {

    @Resource
    private IUserDao userDao;

    @Override
    public User getUserById(int userId)
    {
        return this.userDao.selectByPrimaryKey(userId);
    }

    @Override
    public void deleteUserById(int userId) {

        userDao.deleteByPrimaryKey(userId);
    }

    @Override
    public User updateUserById(User record) {
        userDao.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public int addUser(User user) {
       return userDao.insertSelective(user);

    }

    @Override
    public List<User> findAll() {
        return userDao.selectAll();
    }

    @Override
    public User findById(int userId) {
        return userDao.selectByPrimaryKey(userId);
    }

    @Override
    public String findPasswordByUsername(String username) {
        return userDao.findPasswordByUsername(username);
    }

    @Override
    public Pager findUserByPageIndex(int pageNumber) {
        int pageSize=5;
        int count=userDao.count();
        int allPage=count%pageSize==0?count/pageSize:count/pageSize+1;
        int pageOffset=(pageNumber-1)*pageSize;

        List <User> users=userDao.selectUserByPageIndex(pageOffset,pageSize);
        Pager pager=new Pager();

        pager.setAllPage(allPage);

        pager.setData(users);

        pager.setPageIndex(pageNumber);
        return pager;

    }
}
